package com.minivision.sdktestdemo.ui;

import android.content.res.Resources;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.minivision.sdktestdemo.R;
import com.minivision.sdktestdemo.controler.ui.list.ListControler;
import com.minivision.sdktestdemo.theme.utils.CustomThemeInterface;
import com.minivision.sdktestdemo.theme.utils.ThemeUtils;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by devtang on 2016/4/28.
 * 主音乐列表的适配器
 */
public class MusicItemRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int MUSIC_LIST = 1;

    public static final int CLASSFIED_LIST = 2;

    private int attrTitleColor = -1;

    public interface OnItemListener {
        public void onItemClick(View v, int position);

        public void onMoreItemClick(int position);
    }

    public interface onPlayEventChanged {
        String getKeyUri();
        int getType();
    }

    private ListControler mListControler;
    private onPlayEventChanged mListener;
    private OnItemListener mClickListener;

    public MusicItemRecyclerAdapter(onPlayEventChanged listener, ListControler listControler) {
        mListControler = listControler;
        mListener = listener;
    }

    public void setItemClickListener(OnItemListener listener) {
        mClickListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_music_list_item_with_pic, parent, false);
        return new MusicViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        MusicViewHolder viewHolder = (MusicViewHolder) holder;
        viewHolder.mTitle.setText(mListControler.getTitle(position));
        viewHolder.mContent.setText(mListControler.getContent(position));
        switch (mListener.getType()) {
            case MUSIC_LIST:
                viewHolder.showAlbum(false);
                viewHolder.showAlbumCover(false);
                if (mListControler.isItemPlayed(mListener.getKeyUri(), position)) {
                    viewHolder.mImageIcon.setImageResource(R.drawable.song_play_icon);
                    viewHolder.showIcon(true);
                } else {
                    viewHolder.showIcon(false);
                }
                break;
            case CLASSFIED_LIST:
                viewHolder.showUriIcon(mListControler.getPicUrl(position), mListControler.getType());
                if (mListControler.isItemPlayed(mListener.getKeyUri(), position)) {
                    viewHolder.mImageSound.setVisibility(View.VISIBLE);
                    viewHolder.mImageMore.setVisibility(View.GONE);
                } else {
                    viewHolder.mImageMore.setVisibility(View.VISIBLE);
                    viewHolder.mImageSound.setVisibility(View.GONE);
                }
                break;
        }
        viewHolder.mImageMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickListener.onMoreItemClick(position);
            }
        });
        initItemClickListener(viewHolder.itemView, position);
        ThemeUtils.initTheme(viewHolder.itemView);
    }

    public void initItemClickListener(View v, final int position) {
        if (mClickListener != null) {
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mClickListener.onItemClick(v, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mListControler.size();
    }

    public static class MusicViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_title)
        TextView mTitle;

        @Bind(R.id.tv_content)
        TextView mContent;

        @Bind(R.id.img_icon)
        SimpleDraweeView mImagePlay;

        @Bind(R.id.img_more)
        SimpleDraweeView mImageMore;

        @Bind(R.id.img_back)
        SimpleDraweeView mImageBack;

        @Bind(R.id.img_song)
        SimpleDraweeView mImageIcon;

        @Bind(R.id.img_sound)
        SimpleDraweeView mImageSound;

        @Bind(R.id.layout_item)
        LinearLayout mItemLayout;

        @Bind(R.id.divider)
        View mDivider;

        public MusicViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

//        public void initTheme() {
//            Resources.Theme theme = ThemeUtils.getTheme();
//            if (theme != null) {
//                ((CustomThemeInterface) mDivider).setTheme(theme);
//                ((CustomThemeInterface) mTitle).setTheme(theme);
//                ((CustomThemeInterface) mContent).setTheme(theme);
//                ((CustomThemeInterface) mImageMore).setTheme(theme);
//                ((CustomThemeInterface) mImageIcon).setTheme(theme);
//                ((CustomThemeInterface) mImageSound).setTheme(theme);
//                ((CustomThemeInterface) mItemLayout).setTheme(theme);
//            }
//        }

        public void showView(SimpleDraweeView view, boolean flag) {
            if (flag) {
                if (view.getVisibility() == View.GONE) {
                    view.setVisibility(View.VISIBLE);
                }
            } else {
                if (view.getVisibility() == View.VISIBLE) {
                    view.setVisibility(View.GONE);
                }
            }
        }

        public void showAlbumCover(boolean flag) {
            showView(mImageBack, flag);
        }

        public void showAlbum(boolean flag) {
            showView(mImagePlay, flag);
        }

        public void showIcon(boolean flag) {
            showView(mImageIcon, flag);
        }

        public void showDefaultIcon(int type) {
            switch (type) {
                case ListControler.SINGERS:
                    showAlbumCover(false);
                    showAlbum(true);
                    showIcon(false);
                    mImagePlay.setImageResource(R.drawable.ic_action_user);
                    break;
                case ListControler.ALBUMS:
                    showAlbumCover(true);
                    showAlbum(true);
                    showIcon(false);
                    mImagePlay.setImageResource(R.drawable.placeholder_disk_150);
                    break;
                case ListControler.PATHS:
                    showAlbumCover(false);
                    showAlbum(true);
                    showIcon(false);
                    mImagePlay.setImageResource(R.drawable.ic_action_folder_open);
                    break;
            }
        }

        public void showUriIcon(Uri uri, int type) {
            if (uri != null) {
                showAlbumCover(true);
                showAlbum(true);
                showIcon(false);
                mImagePlay.setImageURI(uri);
            } else {
                showDefaultIcon(type);
            }
        }
    }
}
